package com.bbh.data.service.service.controller;

import com.bbh.common.domain.StatusType;
import com.bbh.common.exception.CustomerException;
import com.bbh.common.utils.Exceptions;
import com.bbh.common.utils.web.spring.SpringUtils;
import com.bbh.data.service.service.constants.ErrorCode;
import com.bbh.data.service.service.utils.ErrorCodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by roman_000 on 2016/5/27.
 */
public class BaseController {
    protected Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected <E> List<E> transformBean(List<?> list, Class<E> eClass) throws CustomerException {
        List<E> eList = new ArrayList<>();
        try {
            if (!CollectionUtils.isEmpty(list)) {
                E e = null;
                for (Object obj : list) {
                    e = eClass.newInstance();
                    BeanUtils.copyProperties(obj,e);
                    eList.add(e);
                }
            }
            return eList;
        } catch (Exception ex) {
            logger.error(Exceptions.getStackTraceAsString(ex));
            throw new CustomerException(ErrorCode.UNKNOWN_ERROR, ex.getMessage());
        }
    }

    protected <E> E transformBean(Object target, Class<E> eClass) throws CustomerException {
        E source;
        try {
            source = eClass.newInstance();
            BeanUtils.copyProperties(target, source);
            return source;
        } catch (Exception ex) {
            logger.error(Exceptions.getStackTraceAsString(ex));
            throw new CustomerException(ErrorCode.UNKNOWN_ERROR, ex.getMessage());
        }
    }
}
